package com.xxxx.server.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.sun.deploy.net.URLEncoder;
import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.RespPageBean;
import com.xxxx.server.pojo.Tdaka;
import com.xxxx.server.service.ITdakaService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 教职工打卡表 前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2022-06-01
 */
@RestController
@RequestMapping("/clo/tea/")
public class TdakaController {

    @Autowired
    private ITdakaService tdakaService;

    @ApiOperation(value = "获取所有教职工打卡信息")
    @GetMapping("/get/all")
    public List<Tdaka> getAllTdaka(){
        return tdakaService.list();
    }

    @ApiOperation(value = "根据TDid获得教职工打卡信息")
    @GetMapping("/get/teadakaall")
    public List<Tdaka> getTdakaByTDid(){
        return tdakaService.getTdakaByTDid();
    }

    @ApiOperation(value = "添加教职工打卡信息")
    @PostMapping("/post/add")
    public RespBean addTdaka(@RequestBody Tdaka tdaka){
        tdaka.setCreatTime(LocalDateTime.now());
        tdaka.setTDid(((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
        if (tdakaService.save(tdaka)){
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败!");
    }

    @ApiOperation(value = "更新教职工打卡信息")
    @PutMapping("/post/update")
    public RespBean updateTdaka(@RequestBody Tdaka tdaka){
        tdaka.setUpdateTime(LocalDateTime.now());
        if (tdakaService.updateById(tdaka)){
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    @ApiOperation(value = "删除教职工打卡信息")
    @DeleteMapping("/get/{id}")
    public RespBean deleteTdaka(@PathVariable Integer id){
        if (tdakaService.removeById(id)){
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "批量删除教职工打卡信息")
    @DeleteMapping("/get")
    public RespBean deleteTdakaByIds(Integer[] ids){
        if (tdakaService.removeByIds(Arrays.asList(ids))){
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "查询所有的教职工打卡（分页）")
    @GetMapping("/get/getPageTdaka/")
    public RespPageBean getTdaka(@RequestParam(defaultValue = "1") Integer currentPage ,
                                    @RequestParam(defaultValue = "10")Integer size,
                                    Tdaka tdaka,
                                    String[] beginDateScope){
        return tdakaService.getTdakaByPage(currentPage,size,tdaka,beginDateScope);

    }

    @ApiOperation(value = "导出教职工打卡数据")
    @GetMapping(value = "/get/export",produces = "application/octet-stream")
    public void exportEmployee(HttpServletResponse response){
        int adminid = ((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
        List<Tdaka> list = tdakaService.getTdaka(null);
        ExportParams params = new ExportParams("教职工打卡","教职工打卡表", ExcelType.HSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(params, Tdaka.class, list);
        ServletOutputStream out = null;
        try {
            //流形式
            response.setHeader("content-type","application/octet-stream");
            //防止中文乱码
            response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode("教职工打卡表.xls","UTF-8"));
            out = response.getOutputStream();
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null!=out){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }




}